import pandas as pd 
import numpy as np
import time
import cx_Oracle
from sqlalchemy import create_engine

#0策略记录表的构造
#df = pd.read_excel('shuju_sample.xlsx')

#######################################考虑为空的情况#





#非两高大中开始


#绿色贷款

def lsdk_ntb(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0
#绿色债券

def lszq_ntb(x):
    if str(x) == 'nan':
        return 0  
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
#绿色企业
def lsqy_ntb(x):
    if x == '深绿':
        return 1
    elif x == '中绿':
        return 0.75
    elif x == '浅绿':
        return 0.5
    elif x == '非绿':
        return 0
    else:
        return 0.5
#绿色工厂
def lsgc_ntb(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0

#环境信用评价

def hjxypj_ntb(x):
    hyjz = x.split(',')[1]  
    dj = x.split(',')[0]                                 # 注意拆解的顺序
    ml = x.split(',')[2]
    if str(dj) in ['1.0','1']:
        return 1
    elif str(dj) in ['2.0','2']:
        return 0.65
    elif str(dj) in ['3.0','3']:
        return 0.35
    elif str(dj) in ['4.0','4']:
        return 0.1
    elif str(dj) in ['5.0','5']:
        return 0 
    elif str(hyjz) != 'nan':
        return np.nan
    elif str(hyjz) == 'nan' and str(ml) in ['B','C','D']:
        return  0.875
    elif str(hyjz) == 'nan' and str(ml) not in ['B','C','D']:
        return 0.9


def hj_cfcs_ntb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if x == 0 :
        return 1
    elif x == 1:
        return 0.5
    elif x == 2:
        return 0.2
    elif int(x) >= 3:
        return  0

def hj_cflx_ntb(x):
    a = str(x.split(',')[0])
    b = x.split(',')[1]
    if str(a) == 'nan' or str(a) == 'None':
        return 1
    elif a == '0'or a == '0.0':
        return 0.7
    elif a == '1'or a == '1.0':
        return 0.8
    elif (a == '2'or a=='2.0') and (float(b) >=0 and float(b)<=50000):
        return 0.7
    elif (a == '2'or a == '2.0') and (float(b) > 50000 and float(b) <= 100000):
        return 0.6
    elif (a == '2'or a == '2.0') and (float(b) > 100000):
        return 0.5
    elif a == '3'or a== '3.0':
        return 0.4
    elif a == '4' or a=='4.0':
        return 0.2
    elif a == '5' or a=='5.0':
        return 0
    elif str(a) == 'nan':
        return 1
        
            
     
 
    






#生态环境严重失信名单
def sthjyzsx_ntb(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1


    


#员工人数变动率


def ygrsbdl_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) < -0.6:
        return 0
    elif float(x) >= -0.6 and float(x) < 0:
        return float(x) / -0.6 * 0.05
    elif float(x) == 0:
        return 0.5
    elif float(x) > 0 and float(x) <= 0.6:
        return float(x) / 0.6 * 0.7
    elif float(x) > 0.6 and float(x) <= 4.6:
        return (float(x)-0.6)/4*0.25+0.7
    elif float(x) > 4.6:
        return 1








#社保违法
def sbwf_ntb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1


#劳动纠纷

def ldjf_ntb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.5
    elif float(x) > 1 and float(x) <= 10:
        return (float(x)-1)/9*(-0.4)+0.5
    elif float(x) > 10:
        return 0
        
        
#人社行政处罚	


def rs_cfcs_ntb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1 :
        return 0
    



def rs_cflx_ntb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2 ' or str(a)=='2.0') and (float(b) >= 0 and float(b) <= 10000) :
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 10000 and float(b) <= 50000):
        return 0.6
    elif str(a) == '2'or str(a)=='2.0' and float(b) > 50000:
        return 0.5
    elif str(a) == '3' or str(a) =='3.0':
        return 0.4
    elif str(a) == '4' or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0
    
#安全生产行政处罚

def aqsc_cfcs_ntb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1:
        return 0

def aqsc_cflx_ntb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) >=0 and float(b) <=20000):
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 20000 and float(b) <= 100000):
        return 0.6
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 100000):
        return 0.5
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0

#产品纠纷
def cpjf_ntb(x):
    if str(x) == 'nan':
        return 1 #如果空认为没有
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
    
# 不正当竞争纠纷
def bzdjzjf_ntb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1  and float(x) < 10:
        return (float(x)-1)/9*(-0.5)+0.5
    elif float(x) >= 10:
        return 0

# 技术创新领先企业
def jscxlxqy_ntb(x):
    if str(x) == 'nan':
        return 0
    if float(x) == 0:
        return 0
    elif float(x) == 1:
        return 0.8
    elif float(x) >= 2:
        return 1





# 制造业单项冠军企业

def zzydxgj_ntb(x):
    if str(x) == 'nan':
        return 0
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0

# 公共信用评价结果

def ggxypjjg_ntb(x):
    if str(x) == 'nan' or str(x) == '' or str(x) == 'None':
        return 0.95
    elif float(x) >= 850:
        return 1
    elif float(x) >= 800 and float(x) < 850:
        return 0.75
    elif float(x) >= 750 and float(x) < 800:
        return 0.5
    elif float(x) >= 700 and float(x) < 750:
        return 0.25
    elif float(x) < 700:
        return 0
    


#股权冻结
def gqdj_ntb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
#股权出质

def gqcz_ntb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
    
#企业类型
def qylx_ntb(x):
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    else:
        return 0

# 出口退税评价
def cktspj_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) >= 0 and float(x) <= 100:
        return float(x)/100
    


# 税务处罚
def swcf_cfcs_ntb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1:
        return 0
    
def swcf_cflx_ntb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif  str(a) == '0'or str(a)=='0.0':
        return 0.75
    elif str(a) == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>=0 and float(b) <=10000):
        return 0.75
    elif (str(a) == '2' or str(a)=='2.0') and (float(b)>10000 and float(b) <=100000):
        return 0.65
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>100000 and float(b) <=1000000):
        return 0.55   
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>1000000):
        return 0.45    
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0



    
# 税务重大违法黑名单
def swzdwfhmd_ntb(x):
    if str(x) == 'nan':
        return 1
    if str(x) == 'Y':
        return 0
    elif str(x) == 'N':
        return 1

    
# 企业成立时间

def qyclsj_ntb(x):
    try:
        if str(x) == 'nan' or x == '' or str(x) == 'None':
            return 0.5
        if float(x) > 0 and float(x) <= 5:
            return (float(x)/5)*0.2
        elif float(x) > 5 and float(x) <= 15:
            return (float(x)-5)/10*0.3+0.2
        elif float(x) >15 and float(x) <= 25:
            return (float(x)-15)/10*0.4+0.5
        elif float(x) > 25:
            return 1
        else:
            return 0.5 #目标与非目标二分
    except Exception as exp: #非数值字符 :else  #正常运行与报错二分
        return 0.5
    


# 注册地与办公地不一致

def zcdybgdbyz_ntb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
# 经营范围变更

def jyfwbg_ntb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
# 亩均效益
def mjxy_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.9
    elif x == 'A':
        return 1
    elif x == 'B':
        return 0.75
    elif x == 'C':
        return 0.5
    elif x == 'D':
        return 0
    


# 负面舆情	

def fmyq_yqcs_ntb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 5:
        return 0.5
    elif float(x) >= 5 and float(x) < 10:
        return 0.2
    elif float(x) >=10:
        return 0
def fmyq_yqlx_ntb(x):
    if str(x) == '未知' or str(x)=='1' or str(x)=='1.0':
        return 0.5
    elif str(x) == '普通新闻'or str(x)=='2' or str(x)=='2.0':
        return 0.83
    elif str(x) == '声誉风险'or str(x)=='3' or str(x)=='3.0':
        return 0.67
    elif str(x) == '经营管理'or str(x)=='4' or str(x)=='4.0':
        return 0.5
    elif str(x) == '公司治理'or str(x)=='5' or str(x)=='5.0':
        return 0.33
    elif str(x) == '财务风险'or str(x)=='6' or str(x)=='6.0':
        return 0.17
    elif str(x) == '信用风险'or str(x)=='7' or str(x)=='7.0':
        return 0
    else:
        return 1

    
# 商业活动旺季

def syhdwj_ntb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 0.3
    elif float(a) == 0:
        return 0
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0 and float(b) <= 0.43):
        return float(b)/0.43*0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.43 and float(b) <= 0.55):
        return (float(b)-0.43)/0.12*0.2+0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.55 and float(b) <= 1):
        return (float(b)-0.55)/0.45*0.3+0.3
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1 and float(b) <= 1.68):
        return (float(b)-1)/0.68*0.3+0.6
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1.68):
        return 1
    elif float(a) == 4:
        return 1




# 不动产抵押	

def bdcdy_ntb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.2
    elif float(x) >= 2:
        return 0
# 海关信用等级

def hgxydj_ntb(x):
    if str(x) == '0':
        return 1
    elif str(x) == '1' or x == 'A':
        return 0.75
    elif x == 'B' or x == 'X':
        return 0.5
    elif x == 'C':
        return 0
    else:
        return 1




# 法律被执行人

def flbzxr_bzxcs_ntb(x):
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 3:
        return 0.5+(3-float(x))/2*(0.2)
    elif float(x) >= 3 and float(x) < 10:
        return (10-float(x))/7*(0.5)
    elif float(x) >= 10:
        return 0
def flbzxr_zxbddze_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif float(x) == 0:
        return 0.8
    elif float(x) > 0 and float(x) <100000:
        return 0.8+(100000-float(x))/100000*(0.2)
    elif float(x) >= 100000 and float(x) <1000000:
        return 0.6+(1000000-float(x))/900000*(0.2)
    elif float(x) >= 1000000 and float(x) <3000000:
        return 0.4+(3000000-float(x))/2000000*(0.2)
    elif float(x) >= 3000000 and float(x) <10000000:
        return 0.2+(10000000-float(x))/7000000*(0.2)
    elif float(x) >= 10000000 and float(x) <100000000:
        return (100000000-float(x))/90000000*(0.2)
    elif float(x) >= 100000000:
        return 0



# 行政处罚	

def xzcf_cfcs_ntb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 3:
        return 0.5+(3-float(x))/2*(0.2)
    elif float(x) >= 3 and float(x) <10:
        return (10-float(x))/7*(0.5)
    elif float(x) >= 10:
        return 0
# 	处罚类型
def xzcf_cflx_ntb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a =='None':
        return 1
    elif str(a) == '0' or str(a) =='0.0':
        return 0.75
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) >= 0 and float(b) <= 10000):
        return 0.75
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) > 10000 and float(b) <= 30000):
        return 0.65
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 30000 and float(b) <= 100000):
        return 0.6
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 100000 and float(b) <= 300000):
        return 0.55
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 300000 ):
        return 0.45
    elif str(a) == '3'or str(a) =='3.0':
        return 0.4
    elif str(a) == '4'or str(a) =='4.0':
        return 0.2
    elif str(a) == '5'or str(a) =='5.0':
        return 0
     

    



# 经营异常
def jyyc_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif '未按照《企业信息公示暂行条例》第八条规定的期限公示年度报告' in str(x) or str(x) == '3.0'or str(x)=='3':
        return 0.7
    elif '未在工商行政管理部门依照《企业信息公示暂行条例》第十条规定责令的期限内公示有关企业信息的' in str(x) or str(x) == '5.0'or str(x)=='5':
        return 0.5
    elif '通过登记的住所或者经营场所无法联系的' in str(x) or str(x) == '4.0'or str(x)=='4':
        return 0.2
    elif '公示企业信息隐瞒真实情况，弄虚作假' in str(x) or str(x) == '2.0'or str(x)=='2':
        return 0
    elif '列入经营异常名录届满3年仍未履行公示义务的，列入严重违法企业名单，自动移出/企业注销，自动移出' in str(x) or str(x) == '1.0'or str(x)=='1':
        return 0






# 财务数据差异率

def cw_fzcyl_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or str(x)=='99999999':
        return 1
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>= 0.01 and float(x) < 0.3:
        return (float(x)-0.01)/(0.30-0.01)*(-0.5) + 1
    elif float(x) >= 0.3 and float(x) <1:
        return (float(x)-0.30)/(1.00-0.30)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0

# 	资产差异率
def cw_zccyl_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or str(x)=='99999999':
        return 0
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>=0.01 and float(x) <0.2:
        return (float(x) - 0.01) / (0.2 - 0.01) * (-0.5) + 1
    elif float(x) >= 0.2 and float(x) < 1:
        return (float(x)-0.20)/(1.00-0.20)*(-0.50)+0.50
    elif float(x) >=1:
        return 0
# 	销售额差异率


def cw_xsecyl_ntb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or str(x) == '99999999':
        return 0 
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x) >= 0.01 and float(x) <= 0.25:
        return (float(x)-0.01)/(0.25-0.01)*(-0.5) + 1
    elif float(x) >= 0.25 and float(x) < 1:
        return (float(x)-0.25)/(1.00-0.25)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0


#非两高大中结束




#非两高小微开始

#绿色贷款

def lsdk_nts(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0
#绿色债券

def lszq_nts(x):
    if str(x) == 'nan':
        return 0  
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
#绿色企业
def lsqy_nts(x):
    if x == '深绿':
        return 1
    elif x == '中绿':
        return 0.75
    elif x == '浅绿':
        return 0.5
    elif x == '非绿':
        return 0
    else:
        return 0.5
#绿色工厂
def lsgc_nts(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0
#环境信用评价

def hjxypj_nts(x):
    hyjz = x.split(',')[1]  
    dj = x.split(',')[0]                                 # 注意拆解的顺序
    ml = x.split(',')[2]
    if str(dj) in ['1.0','1']:
        return 1
    elif str(dj) in ['2.0','2']:
        return 0.65
    elif str(dj) in ['3.0','3']:
        return 0.35
    elif str(dj) in ['4.0','4']:
        return 0.1
    elif str(dj) in ['5.0','5']:
        return 0 
    elif str(hyjz) != 'nan':
        return np.nan
    elif str(hyjz) == 'nan' and str(ml) in ['B','C','D']:
        return  0.875
    elif str(hyjz) == 'nan' and str(ml) not in ['B','C','D']:
        return 0.9


        
        
#生态环境严重失信名单
def sthjyzsx_nts(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1






#社保违法
def sbwf_nts(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1


#产品纠纷
def cpjf_nts(x):
    if str(x) == 'nan':
        return 1 #如果空认为没有
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
    


# 技术创新领先企业
def jscxlxqy_nts(x):
    if str(x) == 'nan':
        return 0
    if float(x) == 0:
        return 0
    elif float(x) == 1:
        return 0.8
    elif float(x) >= 2:
        return 1





# 制造业单项冠军企业

def zzydxgj_nts(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0

# 公共信用评价结果

def ggxypjjg_nts(x):
    if str(x) == 'nan' or str(x) == '' or str(x) == 'None':
        return 0.95
    elif float(x) >= 850:
        return 1
    elif float(x) >= 800 and float(x) < 850:
        return 0.75
    elif float(x) >= 750 and float(x) < 800:
        return 0.5
    elif float(x) >= 700 and float(x) < 750:
        return 0.25
    elif float(x) < 700:
        return 0
    
 
#股权冻结
def gqdj_nts(x):
    if str(x) == 'nan':
        return 0    
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
#股权出质

def gqcz_nts(x):
    if str(x) == 'nan':
        return 0
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
    
#企业类型
def qylx_nts(x):
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    else:
        return 0

# 出口退税评价
def cktspj_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) >= 0 and float(x) <= 100:
        return float(x)/100
    


  # 税务重大违法黑名单
def swzdwfhmd_nts(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1

    
# 企业成立时间

def qyclsj_nts(x):
    try:
        if str(x) == 'nan' or x == '' or str(x) == 'None':
            return 0.5
        if float(x) > 0 and float(x) <= 5:
            return (float(x)/5)*0.2
        elif float(x) > 5 and float(x) <= 15:
            return (float(x)-5)/10*0.4+0.2
        elif float(x) >15 and float(x) <= 25:
            return (float(x)-15)/10*0.3+0.6
        elif float(x) > 25:
            return 1
        else:
            return 0.5 #目标与非目标二分
    except Exception as exp: #非数值字符 :else  #正常运行与报错二分
        return 0.5
        


# 注册地与办公地不一致

def zcdybgdbyz_nts(x):
    if str(x) == 'nan': #如果为空默认一致
        return 1
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
# 经营范围变更

def jyfwbg_nts(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
# 亩均效益
def mjxy_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.9
    elif x == 'A':
        return 1
    elif x == 'B':
        return 0.75
    elif x == 'C':
        return 0.5
    elif x == 'D':
        return 0
      


# 负面舆情	

def fmyq_yqcs_nts(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 5:
        return 0.5
    elif float(x) >= 5 and float(x) < 10:
        return 0.2
    elif float(x) >=10:
        return 0
def fmyq_yqlx_nts(x):
    if str(x) == '未知' or str(x)=='1' or str(x)=='1.0':
        return 0.5
    elif str(x) == '普通新闻'or str(x)=='2' or str(x)=='2.0':
        return 0.83
    elif str(x) == '声誉风险'or str(x)=='3' or str(x)=='3.0':
        return 0.67
    elif str(x) == '经营管理'or str(x)=='4' or str(x)=='4.0':
        return 0.5
    elif str(x) == '公司治理'or str(x)=='5' or str(x)=='5.0':
        return 0.33
    elif str(x) == '财务风险'or str(x)=='6' or str(x)=='6.0':
        return 0.17
    elif str(x) == '信用风险'or str(x)=='7' or str(x)=='7.0':
        return 0
    else:
        return 1










# 不动产抵押	

def bdcdy_nts(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.2
    elif float(x) >= 2:
        return 0
# 海关信用等级

def hgxydj_nts(x):
    if x == '0':
        return 1
    elif x == '1' or x == 'A':
        return 0.75
    elif x == 'B' or x == 'X':
        return 0.5
    elif x == 'C':
        return 0
    else:
        return 1

   



 # 经营异常 --一致？？？
def jyyc_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif '未按照《企业信息公示暂行条例》第八条规定的期限公示年度报告' in str(x) or str(x) == '3.0'or str(x)=='3':
        return 0.7
    elif '未在工商行政管理部门依照《企业信息公示暂行条例》第十条规定责令的期限内公示有关企业信息的' in str(x) or str(x) == '5.0'or str(x)=='5':
        return 0.5
    elif '通过登记的住所或者经营场所无法联系的' in str(x) or str(x) == '4.0'or str(x)=='4':
        return 0.2
    elif '公示企业信息隐瞒真实情况，弄虚作假' in str(x) or str(x) == '2.0'or str(x)=='2':
        return 0
    elif '列入经营异常名录届满3年仍未履行公示义务的，列入严重违法企业名单，自动移出/企业注销，自动移出' in str(x) or str(x) == '1.0'or str(x)=='1':
        return 0


# 财务数据差异率

def cw_fzcyl_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or abs(float(x)) == 99999999:
        return 1
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>= 0.01 and float(x) < 0.3:
        return (float(x)-0.01)/(0.30-0.01)*(-0.5)+1
    elif float(x) >= 0.3 and float(x) <1:
        return (float(x)-0.30)/(1.00-0.30)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0

# 资产差异率
def cw_zccyl_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or abs(float(x)) == 99999999:
        return 1 
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>=0.01 and float(x) <0.2:
        return (float(x) - 0.01) / (0.2 - 0.01) * (-0.5)+1
    elif float(x) >= 0.2 and float(x) < 1:
        return (float(x)-0.20)/(1.00-0.20)*(-0.50)+0.50
    elif float(x) >=1:
        return 0
# 	销售额差异率


def cw_xsecyl_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf'or abs(float(x)) == 99999999:
        return 0
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x) >= 0.01 and float(x) <= 0.25:
        return (float(x)-0.01)/(0.25-0.01)*(-0.5)+1
    elif float(x) >= 0.25 and float(x) < 1:
        return (float(x)-0.25)/(1.00-0.25)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0
      
def hj_cflx_nts(x):
    a = str(x.split(',')[0])
    b = x.split(',')[1]
    if a == '0'or a == '0.0':
        return 0.7
    elif a == '1'or a == '1.0':
        return 0.8
    elif (a == '2'or a=='2.0') and (float(b) >=0 and float(b)<=50000):
        return 0.7
    elif (a == '2'or a == '2.0') and (float(b) > 50000 and float(b) <= 200000):
        return 0.6
    elif (a == '2'or a == '2.0') and (float(b) > 200000):
        return 0.5
    elif a == '3'or a== '3.0':
        return 0.4
    elif a == '4' or a=='4.0':
        return 0.2
    elif a == '5' or a=='5.0':
        return 0
    elif str(a) in[ 'nan','None','']:
        return 1

    
    
def hj_cfcs_nts(x):
    if str(x) == 'nan'or str(x) == 'None':
        return 1
    if float(x) == 0 :
        return 1
    elif float(x) == 1:
        return 0.5
    elif float(x) == 2:
        return 0.2
    elif float(x) >= 3:
        return  0


# (-inf,-0.6)	-1
# [-0.6,-0.3)	(x-(-0.3))/(-0.3)*-0.5-0.4
# [-0.3,0)	x/(-0.3)*-0.4
# 0	0
# (0,0.2]	x/0.2*0.4
# (0.2,0.8]	(x-0.2)/0.6*0.5+0.4
# (0.8,+inf)	1
# NULL	0
#员工人数变动率
def ygrsbdl_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) < -0.6:
        return 0
    elif float(x) >= -0.6 and float(x) < -0.3:
        return (float(x)-(-0.3))/(-0.3) * 0.25+0.3
    elif float(x) == 0:
        return 0.5
    elif float(x) >= -0.3 and float(x) <= 0:
        return float(x)/(-0.3)* 0.3
    elif float(x) > 0 and float(x) <= 0.2:
        return float(x)/0.2*0.7
    elif float(x) >0.2 and float(x) <= 0.8:
        return (float(x)-0.2)/0.6*0.25+0.7
    elif float(x) > 0.8:
        return 1


#劳动纠纷
def ldjf_nts(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1 
    elif float(x) == 1:
        return 0.3
    elif float(x) > 1 :
        return 0

#人社行政处罚

def rs_cflx_nts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2 ' or str(a)=='2.0') and (float(b) >= 0 and float(b) <= 5000) :
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 5000 and float(b) <= 10000):
        return 0.6
    elif str(a) == '2'or str(a)=='2.0' and float(b) > 10000:
        return 0.5
    elif str(a) == '3' or str(a) =='3.0':
        return 0.4
    elif str(a) == '4' or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0

def rs_cfcs_nts(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1 :
        return 0
 
# 安全生产行政处罚
# 处罚类型	处罚类型:0	-0.3

def aqsc_cflx_nts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) >=0 and float(b) <=10000):
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 10000 and float(b) <= 50000):
        return 0.6
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 50000):
        return 0.5
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0


def aqsc_cfcs_nts(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1:
        return 0

# 不正当纠纷

def bzdjzjf_nts(x):
    if str(x) == 'nan':
        return 1
    elif float(x) == 0:
        return 1
    elif float(x) >= 1  and float(x) < 5:
        return (float(x)-1)/4*(-0.5) + 0.5
    elif float(x) >= 5:
        return 0

#税务处罚
# 处罚类型	处罚类型:0	-0.25

def swcf_cflx_nts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif  str(a) == '0'or str(a)=='0.0':
        return 0.75
    elif a == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>=0 and float(b) <=10000):
        return 0.75
    elif (str(a) == '2' or str(a)=='2.0') and (float(b)>10000 and float(b) <=50000):
        return 0.65
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>50000 and float(b) <=200000):
        return 0.6  
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>200000 and float(b) <=1000000):
        return 0.55
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>1000000):
        return 0.45    
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0

# 税务处罚
def swcf_cfcs_nts(x):
    if str(x) == 'nan'or str(x) == 'None':
        return 1
    elif float(x) == 0:
        return 1
    elif float(x) >= 1:
        return 0

# #企业成立时间

def qyclsj_nts(x):
    try:
        if str(x) == 'nan' or x == '' or str(x) == 'None':
            return 0.5
        if float(x) > 0 and float(x) <= 3:
            return (float(x)/3)*0.2
        elif float(x) > 3 and float(x) <= 10:
            return (float(x)-3)/7*0.3+0.2
        elif float(x) >10 and float(x) <= 20:
            return (float(x)-10)/10*0.4+0.5
        elif float(x) > 20:
            return 1
        else:
            return 0.5 #目标与非目标二分
    except Exception as exp: #非数值字符 :else  #正常运行与报错二分
        return 0.5

#商业活动旺季

def syhdwj_nts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 0.3
    elif float(a) == 0:
        return 0
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0 and float(b) <= 0.45):
        return float(b)/0.45*0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.45 and float(b) <= 0.65):
        return (float(b)-0.45)/0.2*0.2+0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.65 and float(b) <= 1):
        return (float(b)-0.65)/0.35*0.3+0.3
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1 and float(b) <= 1.75):
        return (float(b)-1)/0.75*0.3+0.6
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1.75):
        return 1
    elif float(a) == 4:
        return 1


# #法律被执行人


def flbzxr_bzxcs_nts(x):
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 3:
        return 0.5+(3-float(x))/2*(0.2)
    elif float(x) >= 3 and float(x) < 10:
        return (10-float(x))/7*(0.5)
    elif float(x) >= 10:
        return 0
def flbzxr_zxbddze_nts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif float(x) == 0:
        return 0.8
    elif float(x) > 0 and float(x) <100000:
        return 0.75+(100000-float(x))/100000*(0.25)
    elif float(x) >= 100000 and float(x) <1000000:
        return 0.5+(1000000-float(x))/900000*(0.25)
    elif float(x) >= 1000000 and float(x) <5000000:
        return 0.25+(5000000-float(x))/4000000*(0.25)
    elif float(x) >= 5000000 and float(x) <50000000:
        return (50000000-float(x))/45000000*(0.25)
    elif float(x) >= 50000000:
        return 0



#行政处罚
# 处罚次数	0	0

def xzcf_cfcs_nts(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 3:
        return 0.5+(3-float(x))/2*(0.2)
    elif float(x) >= 3 and float(x) <10:
        return 0.25+(10-float(x))/7*(0.25)
    elif float(x) >= 10 and float(x) < 20:
        return (20-float(x))/10*(0.25)
    elif float(x) >= 20:
        return 0
# 处罚类型	处罚类型:0	-0.25

def xzcf_cflx_nts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a =='None':
        return 1
    elif str(a) == '0' or str(a) =='0.0':
        return 0.75
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) >= 0 and float(b) <= 3000):
        return 0.75
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) > 3000 and float(b) <= 10000):
        return 0.7
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 10000 and float(b) <= 30000):
        return 0.65
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 30000 and float(b) <= 100000):
        return 0.55
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 100000 and float(b) <= 300000):
        return 0.5
    elif (str(a) == '2'or str(a) =='2.0') and float(b) > 300000:
        return 0.45
    elif str(a) == '3'or str(a) =='3.0':
        return 0.4
    elif str(a) == '4'or str(a) =='4.0':
        return 0.2
    elif str(a) == '5'or str(a) =='5.0':
        return 0
     


# (0,40)	(x/38)*0.3
# [40,45]	((x-38)/6)*0.3+0.3
# (45,50]	((x-44)/6)*0.4+0.6
# (50,55]	1-(x-50)/5*0.4
# (55,60]	0.6-(x-55)/5*0.3
# (60,+inf)	0
# NULL/ELSE	0.5
def skrnl_nts(x):
    if str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5
    elif float(x) > 0 and float(x) < 40:
        return float(x)/38 * 0.3
    elif float(x) >= 40 and float(x) <= 45:
        return ((float(x)-38)/6)*0.3+0.3
    elif float(x) > 45 and float(x) <= 50:
        return ((float(x)-44)/6)*0.4+0.6
    elif float(x) > 50 and float(x) <= 55:
        return 1-(float(x)-50)/5*0.4
    elif float(x) > 55 and float(x) <= 60:
        return 0.6-(float(x)-55)/5*0.3
    elif float(x) > 60:
        return 0 
    else:
        return 0.5

# Y	1
# N	0
# NULL	0.5
#财务负责人国籍
def cwfzrgj_nts(x):
    if str(x) == 'Y' or str(x) == '1' or str(x) == '1.0':
        return 1
    elif str(x) == 'N' or str(x) == '0' or str(x) == '0.0':
        return 0
    elif str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5



#财务负责人年龄
def cwfzrnl_nts(x):
    if str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5
    elif float(x) > 0 and float(x) <= 30:
        return (float(x)/30)*0.3
    elif float(x) > 30 and float(x) <= 35:
        return ((float(x)-30)/5)*0.3+0.3
    elif float(x) > 35 and float(x) <= 40:
        return ((float(x)-35)/5)*0.4+0.6
    elif float(x) > 40 and float(x) <= 45:
        return 1-(float(x)-40)/5*0.4
    elif float(x) > 45 and float(x) < 55:
        return 0.6-(float(x)-45)/10*0.3
    elif float(x) >= 55:
        return 0

# Y	-1
# N	0
# NULL	-0.4
#财务负责人变更
def cwfzrbg_nts(x):
    if str(x) == 'Y' or str(x) == '1' or str(x) == '1.0':
        return 0
    elif str(x) == 'N' or str(x) == '0' or str(x) == '0.0':
        return 1
    elif str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.6

# Y	1
# N	0
# NULL	0.5
#法人国籍
def frgj_nts(x):
    if str(x) == 'Y' or str(x) == '1' or str(x) == '1.0':
        return 1
    elif str(x) == 'N' or str(x) == '0' or str(x) == '0.0':
        return 0
    elif str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5
    


#非两高小微结束







#两高小微开始



#绿色贷款

def lsdk_yts(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0
#绿色债券

def lszq_yts(x):
    if str(x) == 'nan':
        return 0  
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
#绿色企业
def lsqy_yts(x):
    if x == '深绿':
        return 1
    elif x == '中绿':
        return 0.75
    elif x == '浅绿':
        return 0.5
    elif x == '非绿':
        return 0
    else:
        return 0.5
#绿色工厂
def lsgc_yts(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0

#环境信用评价

def hjxypj_yts(x):
    hyjz = x.split(',')[1]  
    dj = x.split(',')[0]                                 # 注意拆解的顺序
    ml = x.split(',')[2]
    if str(dj) in ['1.0','1']:
        return 1
    elif str(dj) in ['2.0','2']:
        return 0.65
    elif str(dj) in ['3.0','3']:
        return 0.35
    elif str(dj) in ['4.0','4']:
        return 0.1
    elif str(dj) in ['5.0','5']:
        return 0 
    elif str(hyjz) != 'nan':
        return np.nan
    elif str(hyjz) == 'nan' and str(ml) in ['B','C','D']:
        return  0.875
    elif str(hyjz) == 'nan' and str(ml) not in ['B','C','D']:
        return 0.9


        
        
     
 
    






#生态环境严重失信名单
def sthjyzsx_yts(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1




#社保违法
def sbwf_yts(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1





    



#产品纠纷
def cpjf_yts(x):
    if str(x) == 'nan':
        return 1 #如果空认为没有
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
    


# 技术创新领先企业
def jscxlxqy_yts(x):
    if str(x) == 'nan':
        return 0
    if float(x) == 0:
        return 0
    elif float(x) == 1:
        return 0.8
    elif float(x) >= 2:
        return 1





# 制造业单项冠军企业

def zzydxgj_yts(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0

# 公共信用评价结果

def ggxypjjg_yts(x):
    if str(x) == 'nan' or str(x) == '' or str(x) == 'None':
        return 0.95
    elif float(x) >= 850:
        return 1
    elif float(x) >= 800 and float(x) < 850:
        return 0.75
    elif float(x) >= 750 and float(x) < 800:
        return 0.5
    elif float(x) >= 700 and float(x) < 750:
        return 0.25
    elif float(x) < 700:
        return 0
    


#股权冻结
def gqdj_yts(x):
    if str(x) == 'nan':
        return 0    
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
#股权出质

def gqcz_yts(x):
    if str(x) == 'nan':
        return 0
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
    
#企业类型
def qylx_yts(x):
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    else:
        return 0

# 出口退税评价
def cktspj_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) >= 0 and float(x) <= 100:
        return float(x)/100
    




# 税务重大违法黑名单
def swzdwfhmd_yts(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1

    
# 企业成立时间

def qyclsj_yts(x):
    try:
        if str(x) == 'nan' or x == '' or str(x) == 'None':
            return 0.5
        if float(x) > 0 and float(x) <= 5:
            return (float(x)/5)*0.2
        elif float(x) > 5 and float(x) <= 15:
            return (float(x)-5)/10*0.4+0.2
        elif float(x) >15 and float(x) <= 25:
            return (float(x)-15)/10*0.3+0.6
        elif float(x) > 25:
            return 1
        else:
            return 0.5 #目标与非目标二分
    except Exception as exp: #非数值字符 :else  #正常运行与报错二分
        return 0.5
    


# 注册地与办公地不一致

def zcdybgdbyz_yts(x):
    if str(x) == 'nan': #如果为空默认一致
        return 1
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
# 经营范围变更

def jyfwbg_yts(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
# 亩均效益
def mjxy_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.9
    elif x == 'A':
        return 1
    elif x == 'B':
        return 0.75
    elif x == 'C':
        return 0.5
    elif x == 'D':
        return 0
    


# 负面舆情	

def fmyq_yqcs_yts(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 5:
        return 0.5
    elif float(x) >= 5 and float(x) < 10:
        return 0.2
    elif float(x) >=10:
        return 0
def fmyq_yqlx_yts(x):
    if str(x) == '未知' or str(x)=='1' or str(x)=='1.0':
        return 0.5
    elif str(x) == '普通新闻'or str(x)=='2' or str(x)=='2.0':
        return 0.83
    elif str(x) == '声誉风险'or str(x)=='3' or str(x)=='3.0':
        return 0.67
    elif str(x) == '经营管理'or str(x)=='4' or str(x)=='4.0':
        return 0.5
    elif str(x) == '公司治理'or str(x)=='5' or str(x)=='5.0':
        return 0.33
    elif str(x) == '财务风险'or str(x)=='6' or str(x)=='6.0':
        return 0.17
    elif str(x) == '信用风险'or str(x)=='7' or str(x)=='7.0':
        return 0
    else:
        return 1

    







# 不动产抵押	

def bdcdy_yts(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.2
    elif float(x) >= 2:
        return 0
# 海关信用等级

def hgxydj_yts(x):
    if x == '0':
        return 1
    elif x == '1' or x == 'A':
        return 0.75
    elif x == 'B' or x == 'X':
        return 0.5
    elif x == 'C':
        return 0
    else:
        return 1





     

    



# 经营异常 --一致？？？
def jyyc_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif '未按照《企业信息公示暂行条例》第八条规定的期限公示年度报告' in str(x) or str(x) == '3.0'or str(x)=='3':
        return 0.7
    elif '未在工商行政管理部门依照《企业信息公示暂行条例》第十条规定责令的期限内公示有关企业信息的' in str(x) or str(x) == '5.0'or str(x)=='5':
        return 0.5
    elif '通过登记的住所或者经营场所无法联系的' in str(x) or str(x) == '4.0'or str(x)=='4':
        return 0.2
    elif '公示企业信息隐瞒真实情况，弄虚作假' in str(x) or str(x) == '2.0'or str(x)=='2':
        return 0
    elif '列入经营异常名录届满3年仍未履行公示义务的，列入严重违法企业名单，自动移出/企业注销，自动移出' in str(x) or str(x) == '1.0'or str(x)=='1':
        return 0






# 财务数据差异率

def cw_fzcyl_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or abs(float(x)) == 99999999:
        return 1
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>= 0.01 and float(x) < 0.3:
        return (float(x)-0.01)/(0.30-0.01)*(-0.5)+1
    elif float(x) >= 0.3 and float(x) <1:
        return (float(x)-0.30)/(1.00-0.30)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0

# 资产差异率
def cw_zccyl_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or abs(float(x)) == 99999999:
        return 1 
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>=0.01 and float(x) <0.2:
        return (float(x) - 0.01) / (0.2 - 0.01) * (-0.5)+1
    elif float(x) >= 0.2 and float(x) < 1:
        return (float(x)-0.20)/(1.00-0.20)*(-0.50)+0.50
    elif float(x) >=1:
        return 0
# 	销售额差异率


def cw_xsecyl_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf'or abs(float(x)) == 99999999:
        return 0
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x) >= 0.01 and float(x) <= 0.25:
        return (float(x)-0.01)/(0.25-0.01)*(-0.5)+1
    elif float(x) >= 0.25 and float(x) < 1:
        return (float(x)-0.25)/(1.00-0.25)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0


#Y	1
# N	0

def thd_yts(x): # thd    碳惠贷 
    if str(x) == 'nan':
        return 0
    elif str(x) == 'Y' or str(x) == '1.0' or str(x) == '1':
        return 1
    elif str(x) == 'N' or str(x) == '0.0' or str(x) == '0':
        return 0

# 碳效等级:一档	1
# 碳效等级:二档	0.5
# 碳效等级:三档	0
# 碳效等级:四挡	-0.5
# 碳效等级:五档	-1
# 碳效等级:NULL/ELSE;行业门类:B	0.3
# 碳效等级:NULL/ELSE;行业门类:C	0.2
# 碳效等级:NULL/ELSE;行业门类:yts	0.2
# 碳效等级:NULL/ELSE;行业门类:G	1
def qytxsp_yts(x):    # qytxsp    企业碳效水平
    level = x.split(',')[0]
    menlei = x.split(',')[1]
    lst = ['1','1.0','一档','2','2.0','二档','3','3.0','三档','4','4.0','四档','5','5.0','五档']
    if str(level) in ['1','1.0','一档']:
        return 1
    elif str(level) in ['2','2.0','二档']:
        return 0.75
    elif str(level) in ['3','3.0','三档']:
        return 0.5
    elif str(level) in ['4','4.0','四档']:
        return 0.25
    elif str(level) in ['5','5.0','五档']:
        return 0
    elif (str(level) not in lst) and str(menlei) == 'B':
        return 0.65
    elif (str(level) not in lst) and str(menlei) == 'C':
        return 0.6
    elif (str(level) not in lst) and str(menlei) == 'D':
        return 0.6
    elif (str(level) not in lst) and str(menlei) == 'G':
        return 1

# 行业内单位营收能耗表现水平	[0.0，0.1)	1
# 	[0.1，0.5)	0.7
# 	[0.5,0.99)	0.3
# 	[0.99,1.01)	0
# 	[1.01,1.5)	-0.3
# 	[1.5,2.5)	-0.7
# 	(2.5,+)	-1
	#N/A	0
def hyndwysnhbxsp_yts(x): # hyndwysnhbxsp    #行业内单位营收能耗表现水平
    if str(x) in ['nan','None','']:
        return 0.5
    elif float(x) >= 0 and float(x) < 0.1:
        return 1
    elif float(x) >= 0.1 and float(x) < 0.5:
        return 0.85
    elif float(x) >= 0.5 and float(x) < 0.99:
        return 0.65
    elif float(x) >= 0.99 and float(x) < 1.01:
        return 0.5
    elif float(x) >= 1.01 and float(x) <1.5:
        return 0.35
    elif float(x) >= 1.5 and float(x) < 2.5:
        return 0.15
    elif float(x) >= 2.5:
        return 0


# 单位营收能耗增长率	(-,-0.3)	1
# 	[-0.3,-0.1)	0.7
# 	[-0.1,-0.01)	0.3
# 	[-0.01,0.01)	0
# 	[0.01,0.2)	-0.3
# 	[0.2,0.6)	-0.7
# 	[0.6,+)	-1
# 	#N/A	0
def dwysnhzzl_yts(x): # dwysnhzzl    单位营收能耗增长率
    if str(x) in ['nan','None','']:
        return 0.5
    elif float(x) < -0.3:
        return 1
    elif float(x) >= -0.3 and float(x) < -0.1:
        return 0.85
    elif float(x) >= -0.1 and float(x) < -0.01:
        return 0.65
    elif float(x) >= -0.01 and float(x) < 0.01:
        return 0.5
    elif float(x) >= 0.01 and float(x) < 0.2:
        return 0.35
    elif float(x) >= 0.2 and float(x) < 0.6:
        return 0.15
    elif float(x) >= 0.6:
        return 0


#重点排污企业
# Y	-1
# N	0
def zdpwqy_yts(x): # zdpwqy    重点排污企业
    if str(x) == 'nan':
        return 1
    if str(x) == 'Y':
        return 0
    elif str(x) =='N':
        return 1


# skrnl    实控人年龄
# (0,40)	(x/40)*0.3
# [40,45]	((x-40)/5)*0.3+0.3
# (45,50]	((x-45)/5)*0.4+0.6
# (50,55]	1-(x-50)/5*0.4
# (55,60]	0.6-(x-55)/5*0.3
# (60,+inf)	0
# NULL/ELSE	0.5

def skrnl_yts(x):
    if str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5
    elif float(x) > 0 and float(x) < 40:
        return float(x)/40 * 0.3
    elif float(x) >= 40 and float(x) <= 45:
        return ((float(x)-40)/6)*0.3+0.3
    elif float(x) > 45 and float(x) <= 50:
        return ((float(x)-45)/5)*0.4+0.6
    elif float(x) > 50 and float(x) <= 55:
        return 1-(float(x)-50)/5*0.4
    elif float(x) > 55 and float(x) <= 60:
        return 0.6-(float(x)-55)/5*0.3
    elif float(x) > 60:
        return 0 
    else:
        return 0.5 

# Y	1
# N	0
# NULL	0.5
# cwfzrgj    财务负责人国籍
def cwfzrgj_yts(x):
    if str(x) == 'Y' or str(x) == '1' or str(x) == '1.0':
        return 1
    elif str(x) == 'N' or str(x) == '0' or str(x) == '0.0':
        return 0
    elif str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5


# cwfzrnl    财务负责人年龄
def cwfzrnl_yts(x):
    if str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5
    elif float(x) > 0 and float(x) < 35:
        return (float(x)/35)*0.3
    elif float(x) >= 35 and float(x) <= 40:
        return ((float(x)-35)/5)*0.3+0.3
    elif float(x) > 40 and float(x) <= 45:
        return ((float(x)-40)/5)*0.4+0.6
    elif float(x) > 45 and float(x) <= 50:
        return 1-(float(x)-45)/5*0.4
    elif float(x) > 50 and float(x) <= 55:
        return 0.6-(float(x)-50)/5*0.3
    elif float(x) > 55:
        return 0
    else:
        return 0.5

# cwfzrbg    财务负责人变更


def cwfzrbg_yts(x):
    if str(x) == 'Y' or str(x) == '1' or str(x) == '1.0':
        return 0
    elif str(x) == 'N' or str(x) == '0' or str(x) == '0.0':
        return 1
    elif str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.6

# 法人国籍


def frgj_yts(x):
    if str(x) == 'Y' or str(x) == '1' or str(x) == '1.0':
        return 1
    elif str(x) == 'N' or str(x) == '0' or str(x) == '0.0':
        return 0
    elif str(x) == 'nan' or str(x) == 'None' or str(x) == '':
        return 0.5
       
def hj_cflx_yts(x):
    a = str(x.split(',')[0])
    b = x.split(',')[1]
    if a == '0'or a == '0.0':
        return 0.7
    elif a == '1'or a == '1.0':
        return 0.8
    elif (a == '2'or a=='2.0') and (float(b) >=0 and float(b)<=50000):
        return 0.7
    elif (a == '2'or a == '2.0') and (float(b) > 50000 and float(b) <= 200000):
        return 0.6
    elif (a == '2'or a == '2.0') and (float(b) > 200000):
        return 0.5
    elif a == '3'or a== '3.0':
        return 0.4
    elif a == '4' or a=='4.0':
        return 0.2
    elif a == '5' or a=='5.0':
        return 0
    elif str(a) in[ 'nan','None','']:
        return 1

    
    
def hj_cfcs_yts(x):
    if str(x) == 'nan'or str(x) == 'None':
        return 1
    if float(x) == 0 :
        return 1
    elif float(x) == 1:
        return 0.5
    elif float(x) == 2:
        return 0.2
    elif float(x) >= 3:
        return  0

    
def ygrsbdl_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) < -0.6:
        return 0
    elif float(x) >= -0.6 and float(x) < -0.3:
        return (float(x)-(-0.3))/(-0.3) * 0.25 + 0.3
    elif float(x) >= -0.3 and float(x) < 0:
        return float(x)/(-0.3) * 0.3
    elif float(x) == 0:
        return 0.5
    elif float(x) > 0 and float(x) <= 0.2:
        return float(x)/0.2*0.7
    elif float(x) > 0.2 and float(x) <= 0.8:
        return (float(x) - 0.2) / 0.6 * 0.25 + 0.7
    elif float(x) > 0.8:
        return 1

#劳动纠纷
# 0	0
# 1	-0.7
# (1,+inf)	-1
def ldjf_yts(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1 
    elif float(x) == 1:
        return 0.3
    elif float(x) > 1 :
        return 0


#人社行政处罚

def rs_cflx_yts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2 ' or str(a)=='2.0') and (float(b) >= 0 and float(b) <= 5000) :
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 5000 and float(b) <= 10000):
        return 0.6
    elif str(a) == '2'or str(a)=='2.0' and float(b) > 10000:
        return 0.5
    elif str(a) == '3' or str(a) =='3.0':
        return 0.4
    elif str(a) == '4' or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0

def rs_cfcs_yts(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1 :
        return 0

# 安全生产行政处罚
# 处罚类型	处罚类型:0	-0.3

def aqsc_cflx_yts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) >=0 and float(b) <=10000):
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 10000 and float(b) <= 50000):
        return 0.6
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 50000):
        return 0.5
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0


def aqsc_cfcs_yts(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1:
        return 0

# 不正当纠纷

def bzdjzjf_yts(x):
    if str(x) == 'nan':
        return 1
    elif float(x) == 0:
        return 1
    elif float(x) >= 1  and float(x) < 5:
        return (float(x)-1)/4*(-0.5) + 0.5
    elif float(x) >= 5:
        return 0

#税务处罚
# 处罚类型	处罚类型:0	-0.25

def swcf_cflx_yts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif  str(a) == '0'or str(a)=='0.0':
        return 0.75
    elif a == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>=0 and float(b) <=10000):
        return 0.75
    elif (str(a) == '2' or str(a)=='2.0') and (float(b)>10000 and float(b) <=50000):
        return 0.65
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>50000 and float(b) <=200000):
        return 0.6  
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>200000 and float(b) <=1000000):
        return 0.55
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>1000000):
        return 0.45    
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0

# 税务处罚
def swcf_cfcs_yts(x):
    if str(x) == 'nan'or str(x) == 'None':
        return 1
    elif float(x) == 0:
        return 1
    elif float(x) >= 1:
        return 0
#商业活动旺季

def syhdwj_yts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 0.3
    elif float(a) == 0:
        return 0
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0 and float(b) <= 0.45):
        return float(b)/0.45*0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.45 and float(b) <= 0.65):
        return (float(b)-0.45)/0.2*0.2+0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.65 and float(b) <= 1):
        return (float(b)-0.65)/0.35*0.3+0.3
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1 and float(b) <= 1.75):
        return (float(b)-1)/0.75*0.3+0.6
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1.75):
        return 1
    elif float(a) == 4:
        return 1


# 执行标的总额	0	-0.2

def flbzxr_zxbddze_yts(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif float(x) == 0:
        return 0.8
    elif float(x) > 0 and float(x) <100000:
        return 0.8 + (100000-float(x))/100000*(0.2)
    elif float(x) >= 100000 and float(x) <1000000:
        return 0.6+(1000000-float(x))/900000*(0.2)
    elif float(x) >= 1000000 and float(x) <3000000:
        return 0.4 + (3000000-float(x))/2000000*(0.2)
    elif float(x) >= 3000000 and float(x) <10000000:
        return 0.2+(10000000-float(x))/7000000*(0.2)
    elif float(x) >= 10000000 and float(x) <50000000:
        return (50000000-float(x))/40000000*(0.2)
    elif float(x) >= 50000000:
        return 0


def flbzxr_bzxcs_yts(x):
    if str(x) == 'nan':
        return 1
    elif float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 3:
        return 0.5+(3-float(x))/2*0.2
    elif float(x) >= 3 and float(x) < 10:
        return (10-float(x))/7*0.5
    elif float(x) >= 10:
        return 0


# #行政处罚
# 处罚次数	0	0
# 	1	-0.3
# 	2	-0.6
# 	[3,+)	-1
def xzcf_cfcs_yts(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.7
    elif float(x) == 2:
        return 0.4
    elif float(x) >= 3:
        return 0
# 处罚类型	处罚类型:0	-0.3

def xzcf_cflx_yts(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a =='None':
        return 1
    elif str(a) == '0' or str(a) =='0.0':
        return 0.7
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) >= 0 and float(b) <= 5000):
        return 0.7
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) > 5000 and float(b) <= 20000):
        return 0.65
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 20000 and float(b) <= 100000):
        return 0.55
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 100000 ):
        return 0.5
    elif str(a) == '3'or str(a) =='3.0':
        return 0.4
    elif str(a) == '4'or str(a) =='4.0':
        return 0.2
    elif str(a) == '5'or str(a) =='5.0':
        return 0

#两高小微结束




#两高大中开始


#绿色贷款

def lsdk_ytb(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0
#绿色债券

def lszq_ytb(x):
    if str(x) == 'nan':
        return 0  
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
#绿色企业
def lsqy_ytb(x):
    if x == '深绿':
        return 1
    elif x == '中绿':
        return 0.75
    elif x == '浅绿':
        return 0.5
    elif x == '非绿':
        return 0
    else:
        return 0.5
#绿色工厂
def lsgc_ytb(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0
#环境信用评价

def hjxypj_ytb(x):
    hyjz = x.split(',')[1]  
    dj = x.split(',')[0]                                 # 注意拆解的顺序
    ml = x.split(',')[2]
    if str(dj) in ['1.0','1']:
        return 1
    elif str(dj) in ['2.0','2']:
        return 0.65
    elif str(dj) in ['3.0','3']:
        return 0.35
    elif str(dj) in ['4.0','4']:
        return 0.1
    elif str(dj) in ['5.0','5']:
        return 0 
    elif str(hyjz) != 'nan':
        return np.nan
    elif str(hyjz) == 'nan' and str(ml) in ['B','C','D']:
        return  0.875
    elif str(hyjz) == 'nan' and str(ml) not in ['B','C','D']:
        return 0.9


        
        
     
 
    






#生态环境严重失信名单
def sthjyzsx_ytb(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1

    









#社保违法
def sbwf_ytb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1





    



#产品纠纷
def cpjf_ytb(x):
    if str(x) == 'nan':
        return 1 #如果空认为没有
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
    


# 技术创新领先企业
def jscxlxqy_ytb(x):
    if str(x) == 'nan':
        return 0
    if float(x) == 0:
        return 0
    elif float(x) == 1:
        return 0.8
    elif float(x) >= 2:
        return 1






# 制造业单项冠军企业

def zzydxgj_ytb(x):
    if str(x) == 'nan':
        return 0
    elif x == 'Y':
        return 1
    elif x == 'N':
        return 0

# 公共信用评价结果

def ggxypjjg_ytb(x):
    if str(x) == 'nan' or str(x) == '' or str(x) == 'None':
        return 0.95
    elif float(x) >= 850:
        return 1
    elif float(x) >= 800 and float(x) < 850:
        return 0.75
    elif float(x) >= 750 and float(x) < 800:
        return 0.5
    elif float(x) >= 700 and float(x) < 750:
        return 0.25
    elif float(x) < 700:
        return 0
    


#股权冻结
def gqdj_ytb(x):
    if str(x) == 'nan':
        return 0    
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1
#股权出质

def gqcz_ytb(x):
    if str(x) == 'nan':
        return 0
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
    
#企业类型
def qylx_ytb(x):
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    else:
        return 0

# 出口退税评价
def cktspj_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None' :
        return 0.5
    elif float(x) >= 0 and float(x) <= 100:
        return float(x)/100
    






# 税务重大违法黑名单
def swzdwfhmd_ytb(x):
    if str(x) == 'nan':
        return 1
    elif x == 'Y':
        return 0
    elif x == 'N':
        return 1

    
# 企业成立时间

def qyclsj_ytb(x):
    try:
        if str(x) == 'nan' or x == '' or str(x) == 'None':
            return 0.5
        if float(x) > 0 and float(x) <= 5:
            return (float(x)/5)*0.2
        elif float(x) > 5 and float(x) <= 15:
            return (float(x)-5)/10*0.4+0.2
        elif float(x) >15 and float(x) <= 25:
            return (float(x)-15)/10*0.3+0.6
        elif float(x) > 25:
            return 1
        else:
            return 0.5 #目标与非目标二分
    except Exception as exp: #非数值字符 :else  #正常运行与报错二分
        return 0.5
    

# 注册地与办公地不一致

def zcdybgdbyz_ytb(x):
    if str(x) == 'nan': #如果为空默认一致
        return 1
    if x == 'Y':
        return 1
    elif x == 'N':
        return 0
    
# 经营范围变更

def jyfwbg_ytb(x):
    if str(x) == 'nan':
        return 1
    if x == 'Y':
        return 0
    elif x == 'N':
        return 1
# 亩均效益
def mjxy_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.9
    elif x == 'A':
        return 1
    elif x == 'B':
        return 0.75
    elif x == 'C':
        return 0.5
    elif x == 'D':
        return 0
    
  

# 负面舆情	

def fmyq_yqcs_ytb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 5:
        return 0.5
    elif float(x) >= 5 and float(x) < 10:
        return 0.2
    elif float(x) >=10:
        return 0
def fmyq_yqlx_ytb(x):
    if str(x) == '未知' or str(x)=='1' or str(x)=='1.0':
        return 0.5
    elif str(x) == '普通新闻'or str(x)=='2' or str(x)=='2.0':
        return 0.83
    elif str(x) == '声誉风险'or str(x)=='3' or str(x)=='3.0':
        return 0.67
    elif str(x) == '经营管理'or str(x)=='4' or str(x)=='4.0':
        return 0.5
    elif str(x) == '公司治理'or str(x)=='5' or str(x)=='5.0':
        return 0.33
    elif str(x) == '财务风险'or str(x)=='6' or str(x)=='6.0':
        return 0.17
    elif str(x) == '信用风险'or str(x)=='7' or str(x)=='7.0':
        return 0
    else:
        return 1

    







# 不动产抵押	

def bdcdy_ytb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.2
    elif float(x) >= 2:
        return 0
# 海关信用等级

def hgxydj_ytb(x):
    if x == '0':
        return 1
    elif x == '1' or x == 'A':
        return 0.75
    elif x == 'B' or x == 'X':
        return 0.5
    elif x == 'C':
        return 0
    else:
        return 1




     

    




# 经营异常 --一致？？？
def jyyc_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif '未按照《企业信息公示暂行条例》第八条规定的期限公示年度报告' in str(x) or str(x) == '3.0'or str(x)=='3':
        return 0.7
    elif '未在工商行政管理部门依照《企业信息公示暂行条例》第十条规定责令的期限内公示有关企业信息的' in str(x) or str(x) == '5.0'or str(x)=='5':
        return 0.5
    elif '通过登记的住所或者经营场所无法联系的' in str(x) or str(x) == '4.0'or str(x)=='4':
        return 0.2
    elif '公示企业信息隐瞒真实情况，弄虚作假' in str(x) or str(x) == '2.0'or str(x)=='2':
        return 0
    elif '列入经营异常名录届满3年仍未履行公示义务的，列入严重违法企业名单，自动移出/企业注销，自动移出' in str(x) or str(x) == '1.0'or str(x)=='1':
        return 0






# 财务数据差异率

def cw_fzcyl_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf'or str(x) == '99999999':
        return 1
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>= 0.01 and float(x) < 0.3:
        return (float(x)-0.01)/(0.30-0.01)*(-0.5) + 1
    elif float(x) >= 0.3 and float(x) <1:
        return (float(x)-0.30)/(1.00-0.30)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0

# 	资产差异率
def cw_zccyl_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or str(x) == '99999999':
        return 0 
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x)>=0.01 and float(x) <0.2:
        return (float(x) - 0.01) / (0.2 - 0.01) * (-0.5) + 1
    elif float(x) >= 0.2 and float(x) < 1:
        return (float(x)-0.20)/(1.00-0.20)*(-0.50)+0.50
    elif float(x) >=1:
        return 0
# 	销售额差异率


def cw_xsecyl_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif str(x) == 'ERROR'or str(x)=='inf' or str(x)=='-inf' or str(x) == '99999999':
        return 0 
    elif float(x) >= 0.00 and float(x) < 0.01: 
        return 1
    elif float(x) >= 0.01 and float(x) <= 0.25:
        return (float(x)-0.01)/(0.25-0.01)*(-0.5) + 1
    elif float(x) >= 0.25 and float(x) < 1:
        return (float(x)-0.25)/(1.00-0.25)*(-0.50)+0.50
    elif float(x) >= 1:
        return 0

#环境行政处罚

def hj_cflx_ytb(x):
    a = str(x.split(',')[0])
    b = x.split(',')[1]
    if a == '0'or a == '0.0':
        return 0.7
    elif a == '1'or a == '1.0':
        return 0.8
    elif (a == '2'or a=='2.0') and (float(b) >=0 and float(b)<=50000):
        return 0.7
    elif (a == '2'or a == '2.0') and (float(b) > 50000 and float(b) <= 200000):
        return 0.6
    elif (a == '2'or a == '2.0') and (float(b) > 200000):
        return 0.5
    elif a == '3'or a== '3.0':
        return 0.4
    elif a == '4' or a=='4.0':
        return 0.2
    elif a == '5' or a=='5.0':
        return 0
    elif str(a) in[ 'nan','None','']:
        return 1
# 0	1
# 1	0.5
# 2	0.2
# [3,+)	0

    
def hj_cfcs_ytb(x):
    if str(x) == 'nan'or str(x) == 'None':
        return 1
    if float(x) == 0 :
        return 1
    elif float(x) == 1:
        return 0.5
    elif float(x) == 2:
        return 0.2
    elif float(x) >= 3:
        return  0


# 员工人数变动率

def ygrsbdl_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 0.5
    elif float(x) < -0.6:
        return 0
    elif float(x) >= -0.6 and float(x) < 0:
        return float(x) / -0.6 * 0.05
    elif float(x) == 0:
        return 0.5
    elif float(x) >= 0 and float(x) <= 0.6:
        return float(x)/(0.6)* 0.7
    elif float(x) > 0.6 and float(x) <= 1.2:
        return (float(x) - 0.6) / 0.6 * 0.25 + 0.7
    elif float(x) >1.2:
        return 1

#劳动纠纷
# 0	0
# 1	-0.5
# (1,5]	(x-1)/4*-0.4-0.5
# (5,+inf)	-1
def ldjf_ytb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1 
    elif float(x) == 1:
        return 0.5
    elif float(x) > 1 and float(x) <= 5:
        return (float(x)-1)/4*-0.4+0.5
    elif float(x) > 5:
        return 0 

#人社行政处罚
# 处罚类型	处罚类型:0	-0.3

def rs_cflx_ytb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2 ' or str(a)=='2.0') and (float(b) >= 0 and float(b) <= 10000) :
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 10000 and float(b) <= 50000):
        return 0.6
    elif str(a) == '2'or str(a)=='2.0' and float(b) > 50000:
        return 0.5
    elif str(a) == '3' or str(a) =='3.0':
        return 0.4
    elif str(a) == '4' or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0

def rs_cfcs_ytb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1 :
        return 0
#安全生产行政处罚
# 处罚类型	处罚类型:0	-0.3

def aqsc_cflx_ytb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif str(a) == '0'or str(a)=='0.0':
        return 0.7
    elif str(a) == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) >=0 and float(b) <=20000):
        return 0.7
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 20000 and float(b) <= 100000):
        return 0.6
    elif (str(a) == '2'or str(a)=='2.0') and (float(b) > 100000):
        return 0.5
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0


def aqsc_cfcs_ytb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if int(x) == 0:
        return 1
    elif int(x) >= 1:
        return 0
#不正当竞争纠纷
#0	0
# [1,5)	(x-1)/4*-0.5-0.5
# [5,+inf)	-1
def bzdjzjf_ytb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1  and float(x) < 5:
        return (float(x)-1)/4*(-0.5)+0.5
    elif float(x) >= 5:
        return 0

#税务处罚

def swcf_cflx_ytb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 1
    elif  str(a) == '0'or str(a)=='0.0':
        return 0.75
    elif a == '1'or str(a)=='1.0':
        return 0.8
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>=0 and float(b) <=10000):
        return 0.75
    elif (str(a) == '2' or str(a)=='2.0') and (float(b)>10000 and float(b) <=100000):
        return 0.65
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>100000 and float(b) <=500000):
        return 0.6  
    elif (str(a) == '2' or str(a)== '2.0')and (float(b)>500000 and float(b) <=1000000):
        return 0.55
    elif (str(a) == '2'or str(a)=='2.0') and (float(b)>1000000):
        return 0.45    
    elif str(a) == '3'or str(a)=='3.0':
        return 0.4
    elif str(a) == '4'or str(a)=='4.0':
        return 0.2
    elif str(a) == '5'or str(a)=='5.0':
        return 0
# 税务处罚
def swcf_cfcs_ytb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1:
        return 0

#企业成立时间
# (0,10]	(x/10)*0.2
# (10,20]	(x-10)/10*0.4+0.2
# (20,25]	(x-20)/5*0.3+0.6
# (25,+inf)	1
# NULL/ELSE	0.5
def qyclsj_ytb(x):
    try:
        if str(x) == 'nan' or x == '' or str(x) == 'None':
            return 0.5
        if float(x) > 0 and float(x) <= 10:
            return (float(x)/10)*0.2
        elif float(x) > 10 and float(x) <= 20:
            return (float(x)-10)/10*0.4+0.2
        elif float(x) >20 and float(x) <= 25:
            return (float(x)-20)/5*0.3+0.6
        elif float(x) > 25:
            return 1
        else:
            return 0.5 #目标与非目标二分
    except Exception as exp: #非数值字符 :else  #正常运行与报错二分
        return 0.5

#商业活动旺季

def syhdwj_ytb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a == 'None':
        return 0.3
    elif float(a) == 0:
        return 0
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0 and float(b) <= 0.45):
        return float(b)/0.45*0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.45 and float(b) <= 0.65):
        return (float(b)-0.45)/0.2*0.2+0.1
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 0.65 and float(b) <= 1):
        return (float(b)-0.65)/0.35*0.3+0.3
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1 and float(b) <= 1.75):
        return (float(b)-1)/0.75*0.3+0.6
    elif (float(a) >= 1 and float(a) <=3 ) and (float(b) > 1.75):
        return 1
    elif float(a) == 4:
        return 1




#法律被执行人
# 执行标的总额	0	-0.2

def flbzxr_zxbddze_ytb(x):
    if str(x) == 'nan' or x == '' or str(x) == 'None':
        return 1
    elif float(x) == 0:
        return 0.8
    elif float(x) > 0 and float(x) <100000:
        return 0.8+(100000-float(x))/100000*(0.2)
    elif float(x) >= 100000 and float(x) <1000000:
        return 0.6+(1000000-float(x))/900000*(0.2)
    elif float(x) >= 1000000 and float(x) <10000000:
        return 0.4+(10000000-float(x))/9000000*(0.2)
    elif float(x) >= 10000000 and float(x) <30000000:
        return 0.2+(30000000-float(x))/20000000*(0.2)
    elif float(x) >= 30000000 and float(x) <100000000:
        return (100000000-float(x))/70000000*(0.2)
    elif float(x) >= 100000000:
        return 0


def flbzxr_bzxcs_ytb(x):
    if str(x) == 'nan':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) >= 1 and float(x) < 3:
        return 0.5+(3-float(x))/2*0.2
    elif float(x) >= 3 and float(x) < 10:
        return (10-float(x))/7*0.5
    elif float(x) >= 10:
        return 0
    


#行政处罚次数
# 处罚次数	0	0
# 	1	-0.3
# 	2	-0.6
# 	[3,+)	-1
def xzcf_cfcs_ytb(x):
    if str(x) == 'nan' or str(x) == 'None':
        return 1
    if float(x) == 0:
        return 1
    elif float(x) == 1:
        return 0.7
    elif float(x) == 2:
        return 0.4
    elif float(x) >= 3:
        return 0
# 处罚类型	处罚类型:0	-0.25

def xzcf_cflx_ytb(x):
    a = x.split(',')[0]
    b = x.split(',')[1]
    if str(a) == 'nan' or a == '' or a =='None':
        return 1
    elif str(a) == '0' or str(a) =='0.0':
        return 0.75
    elif str(a) == '1'or str(a) =='1.0':
        return 0.8
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) >= 0 and float(b) <= 10000):
        return 0.75
    elif (str(a) == '2'or str(a) =='2.0' )and (float(b) > 10000 and float(b) <= 30000):
        return 0.65
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 30000 and float(b) <= 50000):
        return 0.6
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 50000 and float(b) <= 200000):
        return 0.55
    elif (str(a) == '2'or str(a) =='2.0') and (float(b) > 200000 ):
        return 0.5
    elif str(a) == '3'or str(a) =='3.0':
        return 0.4
    elif str(a) == '4'or str(a) =='4.0':
        return 0.2
    elif str(a) == '5'or str(a) =='5.0':
        return 0
       
       
 # 碳惠贷   thd    碳惠贷   
def thd_ytb(x): # thd    碳惠贷 
    if str(x) == 'nan':
        return 0
    if str(x) == 'Y' or str(x) == '1.0' or str(x) == '1':
        return 1
    elif str(x) == 'N' or str(x) == '0.0' or str(x) == '0':
        return 0

#企业碳效水平  --需要衍生特征 
# 碳效等级:一档	1
# 碳效等级:二档	0.5
# 碳效等级:三档	0
# 碳效等级:四挡	-0.5
# 碳效等级:五档	-1
# 碳效等级:NULL/ELSE;行业门类:B	0.5
# 碳效等级:NULL/ELSE;行业门类:ytb	0.2
# 碳效等级:NULL/ELSE;行业门类:D	0.2
# 碳效等级:NULL/ELSE;行业门类:G	1
def qytxsp_ytb(x):    # qytxsp    企业碳效水平
    level = x.split(',')[0]
    menlei = x.split(',')[1]
    lst = ['1','1.0','一档','2','2.0','二档','3','3.0','三档','4','4.0','四档','5','5.0','五档']
    if str(level) in ['1','1.0','一档']:
        return 1
    elif str(level) in ['2','2.0','二档']:
        return 0.5
    elif str(level) in ['3','3.0','三档']:
        return 0
    elif str(level) in ['4','4.0','四档']:
        return -0.5
    elif str(level) in ['5','5.0','五档']:
        return -1
    elif (str(level) not in lst) and str(menlei) == 'B':
        return 0.5
    elif (str(level) not in lst) and str(menlei) == 'C':
        return 0.2
    elif (str(level) not in lst) and str(menlei) == 'D':
        return 0.2
    elif (str(level) not in lst) and str(menlei) == 'G':
        return 1
    
    
    
#行业内单位营收能耗表现水平	[0.0，0.1)	1

def hyndwysnhbxsp_ytb(x): # hyndwysnhbxsp    #行业内单位营收能耗表现水平
    if str(x) in ['nan','None','']:
        return 0.5
    elif float(x) >= 0 and float(x) < 0.1:
        return 1
    elif float(x) >= 0.1 and float(x) < 0.5:
        return 0.85
    elif float(x) >= 0.5 and float(x) < 0.99:
        return 0.65
    elif float(x) >= 0.99 and float(x) < 1.01:
        return 0.5
    elif float(x) >= 1.01 and float(x) <1.5:
        return 0.35
    elif float(x) >= 1.5 and float(x) < 2.5:
        return 0.15
    elif float(x) >= 2.5:
        return 0
        
#################### qyhn    企业耗能 
# 单位营收能耗增长率	（-，-0.3)	1

def dwysnhzzl_ytb(x): # dwysnhzzl    单位营收能耗增长率
    if str(x) in ['nan','None','']:
        return 0.5
    elif float(x) < -0.3:
        return 1
    elif float(x) >= -0.3 and float(x) < -0.1:
        return 0.85
    elif float(x) >= -0.1 and float(x) < -0.01:
        return 0.65
    elif float(x) >= -0.01 and float(x) < 0.01:
        return 0.5
    elif float(x) >= 0.01 and float(x) < 0.2:
        return 0.35
    elif float(x) >= 0.2 and float(x) < 0.6:
        return 0.15
    elif float(x) >= 0.6:
        return 0

#重点排污企业
# Y	-1
# N	0
def zdpwqy_ytb(x): # zdpwqy    重点排污企业
    if str(x) == 'nan':
        return 1
    if str(x) == 'Y':
        return 0
    elif str(x) =='N':
        return 1

    
# 碳效等级:NULL/ELSE;行业门类:G	1
def qytxsp_ytb(x):    # qytxsp    企业碳效水平
    level = x.split(',')[0]
    menlei = x.split(',')[1]
    lst = ['1','1.0','一档','2','2.0','二档','3','3.0','三档','4','4.0','四档','5','5.0','五档']
    if str(level) in ['1','1.0','一档']:
        return 1
    elif str(level) in ['2','2.0','二档']:
        return 0.75
    elif str(level) in ['3','3.0','三档']:
        return 0.5
    elif str(level) in ['4','4.0','四档']:
        return 0.25
    elif str(level) in ['5','5.0','五档']:
        return 0
    elif (str(level) not in lst) and str(menlei) == 'B':
        return 0.75
    elif (str(level) not in lst) and str(menlei) == 'C':
        return 0.6
    elif (str(level) not in lst) and str(menlei) == 'D':
        return 0.6
    elif (str(level) not in lst) and str(menlei) == 'G':
        return 1

#两高大中结束





#传入两张表 返回一张表
def fill_hjxypj_na(dfs,data,df_fill):                        #传入dfs分数表 和特征表
    print('拼接环境信用评价分与行业门类')
    score = dfs.hjxypj                                       # 取出计算好的分数
    #dfs_hj = pd.concat([score,data.ml],axis=1)               #分数与门类拼接
    dfs_hj = pd.merge(score,data.ml,left_index = True,right_index=True,how='left')
    #display(dfs_hj) 
    #display(df_fill)                              
    dfs_hj = dfs_hj.reset_index()                            #保留rating_id 后续进行连接
    print('将被填充分数数据与填充数据连结')
    df_fill.columns = ['ml','hjxypj_jz']
    df = pd.merge(dfs_hj,df_fill,on='ml')                    #连接填充与被填充数据 保留rating_id 后续进行连接
    
    #display(df)
    print('开始填充')
    df.loc[df.hjxypj.isnull(),'hjxypj'] = df.hjxypj_jz       #填充
    
    df.drop(['ml','hjxypj_jz'],axis=1,inplace=True)          #删除辅助列   
    print('分数被填充后删除无用列')
    #display(df)
    print('删除原有分数')
    dfs.drop(['hjxypj'],axis=1,inplace=True)                 #删掉原有的分数
    #display(dfs)
    print('将被填充分数拼接回分数表')
    return pd.merge(dfs,df,left_index=True,right_on='rating_id')#将填充好的分数拼回到dfs 并返回dfs 



#金综分数填充

def fill_jinzong_score(dfs_conn_add,df): # 传入dfs_conn_add 返回填充金综均值后的 dfs_conn_add
    #添加model_type
    df_conn = df.reset_index()[['id_number','model_type']]                   #选取信用代码为联接建
    dfs_new = pd.merge(dfs_conn_add,df_conn,on='id_number',how = 'left')     #已经联接完模型类型 金综小分表
        
    #填充均值 
    if len(dfs_conn_add) > 1000:                                             #如果数据量大于1000则动态填充
        #计算金综均值填充表
        dfs_fill = dfs_new[['e_score','s_score','g_score','model_type']].groupby('model_type').mean().reset_index()
        for col in ['e_score','s_score','g_score']:
            for mt in ['ntb','nts','ytb','yts']:
                if mt in dfs_new.model_type.values: #有这个类型才进行填充
                    dfs_new.loc[(dfs_new[col].isna()) & (dfs_new.model_type == mt),col] = dfs_fill.loc[dfs_fill.model_type == mt,col].values[0]
    else:                                                                    #单条的采取静态填充 月底会自动更新均值
        dfs_new.e_score.fillna(6.34,inplace = True)
        dfs_new.s_score.fillna(2.17,inplace = True)
        dfs_new.g_score.fillna(3.44,inplace = True)
    del dfs_new['model_type']
    return dfs_new


#静态填充
def fill_jinzong_score_static(dfs_conn_add,df,dfs_fill):
    df_conn = df.reset_index()[['id_number','model_type']]                   #选取信用代码为联接建
    dfs_new = pd.merge(dfs_conn_add,df_conn,on='id_number',how = 'left')     #已经联接完模型类型 金综小分表    
    for col in ['e_score','s_score','g_score']:
            for mt in ['ntb','nts','ytb','yts']:
                if mt in dfs_new.model_type.values:                          #有这个类型才进行填充
                    dfs_new.loc[(dfs_new[col].isna()) & (dfs_new.model_type == mt),col] = dfs_fill.loc[dfs_fill.model_type == mt,col].values[0]
    del dfs_new['model_type']
    return dfs_new       

#分数转换函数
def transform_score(df,model_type,df_w=None,df_fill=None):
    if len(df) == 0:
        return pd.DataFrame()
    #2出小分
    dfs = pd.DataFrame()
    lst = []#存储非apply特征
    dic_customer = {'ytb':'"两高大中客户"','yts':'"两高小微客户"','ntb':'"非两高大中客户"','nts':'"非两高小微客户"'}
    print('{}分数转换开始'.format(dic_customer[model_type]))   
    for col in df.columns:
        try:#带时候放个共性列表
            dfs[col] = df[col].apply(eval(col+'_'+model_type)) #去查有多少一样的有多少不一样的
        except Exception as exp: #筛选掉没有apply的特征
            lst.append(col)
    
    #环境信用评价的均值
    print('开始填充环境信用评价均值')
    dfs = fill_hjxypj_na(dfs,df,df_fill).set_index('rating_id')        #rating_id 放到索引位置否则后续乘权重时对不上
    #display(dfs)
    #3分数聚合
    #聚合时列的数量会先增加后减少
    # dfs.columns
    dfs2 = dfs.copy() #保留最原始小分数
    print('{}分数开始聚合'.format(dic_customer[model_type]))
    #分数聚合
    dfs['hj_xzcf'] = 0.6 * dfs.hj_cflx + 0.4 * dfs.hj_cfcs
    dfs['rs_xzcf'] = 0.6 * dfs.rs_cflx + 0.4 * dfs.rs_cfcs
    dfs['aqsc_xzcf'] =0.6 * dfs.aqsc_cflx + 0.4 * dfs.aqsc_cfcs
    dfs['swcf'] = 0.6 * dfs.swcf_cflx + 0.4 * dfs.swcf_cfcs
    dfs['xzcf'] = 0.6 * dfs.xzcf_cflx + 0.4 * dfs.xzcf_cfcs
    dfs['fmyq'] = 0.6 * dfs.fmyq_yqcs + 0.4 * dfs.fmyq_yqlx
    dfs['cwsjcyl'] = (dfs.cw_fzcyl + dfs.cw_xsecyl +  dfs.cw_zccyl)/3
    #两高才会有
    if model_type in {'ytb','yts'} :
        dfs['qynh'] = 0.5 * dfs.hyndwysnhbxsp + 0.5 * dfs.dwysnhzzl  # del hyndwysnhbxsp del  dwysnhzzl
    #聚合法律被执行人
    temp = 0.5 * df.flbzxr_zxbddze.apply(eval('flbzxr_zxbddze_'+model_type)) + 0.5 * df.flbzxr_bzxcs.apply(eval('flbzxr_bzxcs_'+model_type))
    temp.name = 'temp_'
    temp.to_frame().loc[df.flbzxr_lrsxbs == 'Y','temp_'] = 0  #temp长度与df长度一致
    dfs['flbzxr'] = temp.values
    dfs3 = dfs.copy()#小分+聚合分
    print('聚合结束开始删除无用的辅助分数')
    #聚合后删除小分
    for col in {'fmyq_yqcs','fmyq_yqlx','cw_fzcyl','cw_xsecyl','cw_zccyl','hj_cflx','hj_cfcs','rs_cflx','rs_cfcs','swcf_cflx','swcf_cfcs','xzcf_cflx','xzcf_cfcs','aqsc_cflx','aqsc_cfcs','flbzxr_bzxcs', 'flbzxr_lrsxbs', 'flbzxr_zxbddze','dwysnhzzl','hyndwysnhbxsp','flbzxr_zxbddze_'}:
        if col in dfs.columns:
            del dfs[col]
    
    #排列顺序
    lst1 = dfs.columns.to_list()
    lst1.sort()
    dfs = dfs[lst1]
    #print(dfs.columns)
    print('获取权重并排序')
    df_w = df_w.sort_values('eng_name')
    #print(df_w.eng_name)
    
    #乘上权重
    print('分数开始赋予权重')
    score_df = dfs * (df_w.weight.values ) # 权重需要排序哦   
    print('{}特征分数计算完毕'.format(dic_customer[model_type]),time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
    print(' ')
    return score_df,dfs,dfs2,dfs3,lst#score_df（仅此带权重） dfs 乘权重的前一步 df3有全部分数 用于模型验证 df2只有未聚合分数 异常捕捉列表






#hjxypj均生成函数
def create_df_fill(df,dfs_NoWeig):
    df_cut = df.loc[df.model_type.isin(['ytb','yts','ntb','nts'])]
    df_fill_temp = pd.merge(dfs_NoWeig[['hjxypj']],df_cut[['ml']],left_index=True,right_index=True,how='left')
    df_fill = df_fill_temp.reset_index(drop=True).groupby(['ml']).hjxypj.mean().reset_index()
    return df_fill
    
    

    
